﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _006_VideoDownload
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// html解析对象
        /// </summary>
        private HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

        private string webUrl;
        public Form1()
        {
            InitializeComponent();
            this.Url.Text = "http://www.852ne.com/shipin/list-%E5%9B%BD%E4%BA%A7%E7%B2%BE%E5%93%81.html";
            webUrl = "http://www.852ne.com";
        }

        private void Start_Click(object sender, EventArgs e)
        {
            try
            {
                string url = this.Url.Text;
                List<ImageInfo> imageInfoList = GetPageItemNameUrlDic(url);

                foreach (var item in imageInfoList)
                {
                    this.msg.Text += $"{item.Name}   {item.DateTime.ToLongDateString().ToString()}\n{item.ThunderUrl}\n--------------------------------\n";
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 获取单页的数据格式为名称和对应的磁力链接
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public List<ImageInfo> GetPageItemNameUrlDic(string url)
        {
            string html = HttpHelper.Get(url);
            htmlDoc.LoadHtml(html);
            HtmlNodeCollection listImageUrl = htmlDoc.DocumentNode.SelectNodes("//li[@class='content-item']");
            List<ImageInfo> imageInfoList = new List<ImageInfo>();
            foreach (HtmlNode item in listImageUrl)
            {
                HtmlNode nextUrl = item.ChildNodes.ToList().Where(it => it.Name == "a").FirstOrDefault();
                if (nextUrl != null)
                {
                    string time = nextUrl.ChildNodes[5].InnerHtml;
                    if (DateTime.TryParse(time, out DateTime result) && ChooseTime.Value.Month <= result.Month)
                    {
                        imageInfoList.Add(new ImageInfo()
                        {
                            Name = nextUrl.Attributes[3].Value,
                            Url = webUrl + nextUrl.Attributes[2].Value,
                            DateTime = result
                        });
                    }
                }
            }
            GetPageItemNameThunderDic(imageInfoList);
            return imageInfoList;
        }

        /// <summary>
        /// 根据url获取对应的磁力链接
        /// </summary>
        /// <param name="dicNameUrl"></param>
        /// <returns></returns>
        private void GetPageItemNameThunderDic(List<ImageInfo> imageInfoList)
        {
            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            foreach (var item in imageInfoList)
            {
                string url = item.Url;
                string html = HttpHelper.Get(url);
                if (string.IsNullOrEmpty(html))
                {
                    continue;
                }
                htmlDoc.LoadHtml(html);
                HtmlNode htmlNode = htmlDoc.DocumentNode.SelectNodes("//input[@class='form-control input-sm copy_btn app_disable']").FirstOrDefault();
                if (htmlNode != null)
                {
                    item.ThunderUrl = htmlNode.Attributes[4].Value;
                }
            }
        }
    }
}
